package com.ruoyi.dormitory.service.impl;

import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.exception.UtilException;
import com.ruoyi.dormitory.domain.DormitoryRecord;
import com.ruoyi.dormitory.mapper.DormitoryRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.dormitory.mapper.DormitoryMapper;
import com.ruoyi.dormitory.domain.Dormitory;
import com.ruoyi.dormitory.service.IDormitoryService;
import com.ruoyi.common.core.text.Convert;

/**
 * 宿舍Service业务层处理
 *
 * @author ruoyi
 * @date 2023-04-26
 */
@Service
public class DormitoryServiceImpl implements IDormitoryService {
    @Autowired
    private DormitoryMapper dormitoryMapper;

    @Autowired
    private DormitoryRecordMapper dormitoryRecordMapperMapper;

    /**
     * 查询宿舍
     *
     * @param
     * @return 宿舍
     */
    @Override
    public Dormitory selectDormitoryByDormitoryNumber(Integer dormitoryId) {
//        return dormitoryMapper.selectDormitoryByDormitoryNumber(dormitoryId);
        return dormitoryMapper.selectById(dormitoryId);
    }

    /**
     * 查询宿舍列表
     *
     * @param dormitory 宿舍
     * @return 宿舍
     */
    @Override
    public List<Dormitory> selectDormitoryList(Dormitory dormitory) {
        List<Dormitory> dormitoryList = dormitoryMapper.selectDormitoryList(dormitory).stream().map(dormitory1 -> {
            List<DormitoryRecord> dormitoryRecordList = dormitoryRecordMapperMapper.selectList(new LambdaQueryWrapper<DormitoryRecord>().eq(DormitoryRecord::getDormitoryNumber, dormitory1.getDormitoryId()));
            long size = dormitoryRecordList.size();
            System.out.println("size = " + size);
            dormitory1.setFreeBed(dormitory1.getGalleryful() - size);
            return dormitory1;
        }).collect(Collectors.toList());
        return dormitoryList;
//        return dormitoryMapper.selectDormitoryList(dormitory);
    }

    /**
     * 新增宿舍
     *
     * @param dormitory 宿舍
     * @return 结果
     */
    @Override
    public int insertDormitory(Dormitory dormitory) {
        Dormitory dormitory1 = dormitoryMapper.selectOne(new LambdaQueryWrapper<Dormitory>().eq(Dormitory::getDormitoryNumber, dormitory.getDormitoryNumber()));
        if (dormitory1 != null) {
            throw new UtilException("已有此房间!");
        }
        return dormitoryMapper.insert(dormitory);
    }

    /**
     * 修改宿舍
     *
     * @param dormitory 宿舍
     * @return 结果
     */
    @Override
    public int updateDormitory(Dormitory dormitory) {
        Dormitory dormitory1 = dormitoryMapper.selectOne(new LambdaQueryWrapper<Dormitory>().eq(Dormitory::getDormitoryNumber, dormitory.getDormitoryNumber()));
        if (dormitory1 != null) {
            throw new UtilException("已有此房间!");
        }
        return dormitoryMapper.updateById(dormitory);
    }

    /**
     * 批量删除宿舍
     *
     * @param
     * @return 结果
     */
    @Override
    public int deleteDormitoryByDormitoryNumbers(String dormitoryIds) {
        return dormitoryMapper.deleteDormitoryByDormitoryNumbers(Convert.toStrArray(dormitoryIds));
    }

    /**
     * 删除宿舍信息
     *
     * @param
     * @return 结果
     */
    @Override
    public int deleteDormitoryByDormitoryNumber(Integer dormitoryId) {
        return dormitoryMapper.deleteDormitoryByDormitoryNumber(dormitoryId);
    }
}
